package com.jawmask.android.discover.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.location.Address;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.jawmask.android.discover.R;
import com.jawmask.android.discover.bean.GPSLocation;
import com.jawmask.android.discover.bean.task.GetUserLocationAddressTask;
import com.jawmask.android.discover.utils.UserSession;

public class PlaceSearchActivity extends BaseMapActivity implements OnClickListener {

	private EditText userSearchInput, userLocationAddress;
	private Button searchButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.place_search);

		findViewById(R.id.getMyPlace).setOnClickListener(this);

		userLocationAddress = (EditText) findViewById(R.id.findwhere);
		userSearchInput = (EditText) findViewById(R.id.findwhat);
		searchButton = (Button) findViewById(R.id.buttonFind);
		searchButton.setOnClickListener(this);
	}

	protected void updateUserAddress(final Address userAddress) {
		userLocationAddress.setText(parseAddress(userAddress));
		UserSession.getInstance().setUserAddress(userAddress);
		searchButton.setEnabled(true);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.buttonFind:
			searchButtonOnClick();
			break;
		case R.id.getMyPlace:
			findMyAddress();
			break;
		case R.id.about_us:
			break;
		}
	}

	public void searchButtonOnClick() {
		List<String> validationMessages = validate();
		if (validationMessages.isEmpty()) {
			GPSLocation gpsLocation = new GPSLocation();
			gpsLocation.setAddress(userLocationAddress.getText().toString());
			getValidateUserAddressTask().execute(gpsLocation);
		} else {
			StringBuffer errorMessageBuffer = new StringBuffer();
			for (String errorMessage : validationMessages) {
				errorMessageBuffer.append("* " + errorMessage + "\n");
			}
			showErrorMessageToast(errorMessageBuffer.toString());
		}
	}

	private List<String> validate() {
		List<String> errorMessage = new ArrayList<String>();
		if (userSearchInput.length() == 0) {
			errorMessage.add(getString(R.string.required_search_place));
		}

		if (userLocationAddress.length() == 0) {
			errorMessage.add(getString(R.string.required_search_place_address));
		}
		return errorMessage;
	}

	private void findMyAddress() {
		waitForData(getString(R.string.notification_msg_get_location));
		enableMyLocation();
	}

	private void startSearchResultActivity() {
		Intent intent = new Intent(PlaceSearchActivity.this, SearchResultActivity.class);
		intent.putExtra(getString(R.string.extra_key_search_plcae_user_input), userSearchInput.getText().toString());
		startActivity(intent);
	}

	private GetUserLocationAddressTask getValidateUserAddressTask() {
		return new GetUserLocationAddressTask(this) {

			protected void onPreExecute() {
				waitForData(getString(R.string.notification_msg_validate_address));
			}

			protected void onPostExecute(final Address userAddress) {
				receivedData();
				if (userAddress != null) {
					UserSession.getInstance().setUserAddress(userAddress);
					startSearchResultActivity();
				} else {
					showErrorMessageToast(getString(R.string.invalid_user_address));
				}
			}
		};
	}
}